package LoadUnit
{
	import BasicEngine.Dispatcher;
	import DataStore.BmpDataPool;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import GameLogic.ENUM;
	import GameLogic.Game;
	import GameLogic.LuaEditer;
	

	public class LoadManager
	{
        public static var  ShowLoader:Boolean = false;		
		
		private static var loadTaskQueue:Vector.<UrlLoadTask> = new Vector.<UrlLoadTask>();
		private static var loadLock:Boolean;
		private static var loadline:LoadLine;
		private static var timer:int;
		private static var files:int;
			
		public function LoadManager()
		{
			
		}
		
		/**
		 * 图片资源初始化,同时利用脚本里面的数据,铺设了地图
		 */
		public static function Init():void
		{
			AddLoadTask("Resource/Images/Map/maptile.png", ENUM.LOADIMGTYPE, function(_bmp:BitmapData):void
				{
					BmpDataPool.AddImgToPool(_bmp, "Map", BmpDataPool.LargeSource);
				});
			AddLoadTask("Resource/Images/RoboImg/RobotAA.png", ENUM.LOADIMGTYPE, function(_bmp:BitmapData):void
				{
					BmpDataPool.AddImgToPool(_bmp, "Robot", BmpDataPool.LargeSource);
				});
			AddLoadTask("Resource/lua/Init.txt", ENUM.LOADLUATYPE, function(_st:String):void
				{		
					Game.LuaDict["Init"] = _st;
					Game.LuaAl.doString(_st);
					Game.LuaAl.doString("AddImgToPool()");
				})
		}
		
		/**
		 * 这是一个脚本方法,供脚本调用里面的图片Url数据,通过这个方法,放到资源库
		 * 如果要修改脚本方法,需要同时修改Lua脚本里面的方法
		 */
		public static function ScriptLoadEdit(_url:String):void
		{
			var url:String = "resource/images/luaedit/" + _url;
		
			LoadManager.AddLoadTask(url, ENUM.LOADIMGTYPE, function(_bmp:BitmapData):void
				{
					BmpDataPool.AddImgToEditPool(_bmp);
				});
		}
		
		public static function Update():void
		{
			if ((loadTaskQueue.length > 0) || ShowLoader)
			{
				if (loadline == null)
				{
				  files = loadTaskQueue.length;
				  AddLoader(Game.Width / 2 -70, Game.Height / 2 -50);
				}
			}
			else if(loadTaskQueue.length == 0 && !ShowLoader)
			{
				if (loadline != null)
				{
			       RemoveLoader();
				   files = 0;
				}
			}	
			
			if (loadTaskQueue.length > 0)
			{
				if (!loadLock)
				{
					var p:int = Number(files - loadTaskQueue.length) / Number(files) * 100;
					loadline.totalfileNum.text = p.toString() + "%";

				    if (Dispatcher.Clock.SystemTime - timer > 40)
				    {
				       var task:UrlLoadTask = loadTaskQueue.shift();
				       LoadTask(task);
					   timer = Dispatcher.Clock.SystemTime;
				    }
				}
			}
			else
			{
				if(loadline != null)
				   loadline.totalfileNum.visible = false;
			}
		}
		
		public static function AddLoader(_x:int, _y:int):void
		{
			loadline = new LoadLine();
			Game.MenuCanvas.addChild(loadline);
			loadline.x = _x;
			loadline.y = _y;
		}
		
		public static function RemoveLoader():void
		{
			loadline.Dispose();
			Game.MenuCanvas.removeChild(loadline);
			loadline = null;
		}
		
		public static function AddLoadTask(_url:String,_type:String,_parseResult:Function):void
		{
			var task:UrlLoadTask = new UrlLoadTask(_url, _parseResult, _type);
			loadTaskQueue.push(task);
		}
		
		public static function SetShowLoader(_s:Boolean):void
		{
			ShowLoader = _s;
		}
		
		private static function LoadTask(_task:UrlLoadTask):void
		{
			var url:String = _task.url;
			var type:String = _task.type;
			loadLock = true;
			if (type == ENUM.LOADIMGTYPE)
			{
				var loader:Loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void
				{
					loadLock = false;
					var l:Loader = e.target.loader as Loader;
					var bmpData:BitmapData = (l.content as Bitmap).bitmapData;
					_task.parseLoad(bmpData);
				});
				loader.load(new URLRequest(url));
			}
			else if (type == ENUM.LOADLUATYPE)
			{
				var l:URLLoader = new URLLoader();
				l.dataFormat = URLLoaderDataFormat.TEXT;
				var urlRequest:URLRequest = new URLRequest(url);
				l.addEventListener(Event.COMPLETE, function(e:Event):void
				{
					loadLock = false;
					_task.parseLoad(l.data);
				});
				l.load(urlRequest);
			}
			else if(type == ENUM.LOADURLTYPE)
			{
			}
		}
		
		public static function Incrementfiles():void
		{
			files++;
		}
		
		private static function AddRobot(_bmp:BitmapData):void
		{
			
		}
	}
}